package com.kcn.dbservices;

import java.util.List;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.cfg.AnnotationConfiguration;
import com.kcn.cus.Customer;

import com.kcn.utils.DButils;

public class CurdService {
//	public static void main(String arg[]) {
//		Customer cus = new Customer();
//		cus.setCusaddress("没有人");
//		new CurdService().save(cus);
//	}

	public int save(Customer cus) {
		Session session = DButils.getSession();
		try {
			session.getTransaction().begin();
			// session.save(cus);
			session.save(cus);
			session.getTransaction().commit();
		} catch (HibernateException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		session.close();
		return 0;
	}

	public int update(Customer cus) {
		Session session = DButils.getSession();
		try {
			session.getTransaction().begin();
			// session.save(cus);
			session.update(cus);
			session.getTransaction().commit();
		} catch (HibernateException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		session.close();
		return 0;
	}

	public Customer readbyid(int id) {
		Session session = DButils.getSession();
		session.getTransaction().begin();
		// cus=session.get(arg0, arg1);
		session.getTransaction().commit();
		session.close();
		return null;
	}

	public List readAll() {
		Session session = null;
		List list = null;
		System.out.println("111");
		try {
			System.out.println("222");
			session = DButils.getSession();
			System.out.println("333");
			session.getTransaction().begin();
			list = session.createQuery("from Customer").list();
			session.getTransaction().commit();
		} catch (HibernateException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			session.close();
		}
		return list;
	}

	public int getTotal(String SQL) {
		Session ss = null;
		int total = 0;

		try {
			ss = DButils.getSession();
			ss.getTransaction().begin();
			System.out.println(ss);
			total = Integer.parseInt(ss.createQuery("select count(*) " + SQL)
					.iterate().next().toString());
			ss.getTransaction().commit();
		} catch (HibernateException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		ss.close();
		System.out.println(total);
		return total;
	}


	public List getbypage(int start, int length, String SQL) {
		Session session = null;
		List list = null;
		session = DButils.getSession();
		try {
			session.getTransaction().begin();
			Query q = session.createQuery(SQL);
			q.setFirstResult(start);
			q.setMaxResults(length);
			list = q.list();
			session.getTransaction().commit();
		} catch (HibernateException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		session.close();
		return list;
	}

	public int delete(Customer cus) {
		Session session = DButils.getSession();
		try {
			session.getTransaction().begin();
			session.delete(cus);
			session.getTransaction().commit();
		} catch (HibernateException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		session.close();
		return 0;
	}
}
